<?
include "DB/Database.class.php";
include "Grid.class.php";
include "GridSorter.class.php";


class GridPager{
	public static function PaginateQuery($sql, $pag = 1, $tam_pag = 30){
	
	
		if(!$pag) $pag=1;
	
		$res = DataBase::ExecuteAndReturnResult($sql);
		$nrows = mysql_num_rows($res);
		$npaginas = ceil($nrows / $tam_pag);

		$liminf = ($pag -1) * $tam_pag;
		$limsup = $liminf + $tam_pag;
		
		return $sql .= " LIMIT $liminf, $tam_pag";
	}
	
	public static function GetNavigationControlsHTML($sql, $tam_pag = 30){
	
		$res = DataBase::ExecuteAndReturnResult($sql);
		$nrows = mysql_num_rows($res);
		$npaginas = ceil($nrows / $tam_pag);
		
	

			$html = "<div align='center'>";
			for ($i = 1; $i <= $npaginas; $i++) 
				$html .= "<a href='" . $PHP_SELF . "?pag=$i'>$i</a> -";
			$html = substr($html,0,strlen($html)-1); #stripping the last dash
			return $html .= "</div>";
	}
}


$sql = "SELECT * FROM socios";
echo $sql2 = GridPager::PaginateQuery($sql, 2);
echo GridPager::GetNavigationControlsHTML($sql);
?>